/**
 * 
 */
package org.sintec.AndroidCarConnectivity;

import java.io.IOException;
import java.util.Observable;

import ru.dzakhov.Settings;
import ru.dzakhov.TcpServer;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

/**
 * @author Sintec
 *
 */
public class serviceConnectivity extends Service
{
	public static final int MSG_DEFAULT = 0;
	public static final int MSG_SHOW_NOTIFICATION_MESSAGE = 1;
	private static final String TAG = "ConnectivityService";
	
	private String mBtAddress = "";
	private int mSocketPort = 0;
	private int mHttpPort = 0;
	
	private ConnectivityManager mConnectivityManager = null;

	private NotificationManager mNM;

    // Unique Identification Number for the Notification.
    // We use it on Notification start, and to cancel it.
    private int NOTIFICATION = R.string.connectivity_service_started;
    
    public void setBtAddress(String address)
    {
    	mBtAddress = address;
    }
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        
    	mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // Display a notification about us starting.  We put an icon in the status bar.
        showNotification();
    }
    
    @Override
    public void onDestroy()
    {
    	showCustomNotification("Service destroyed");
    	mConnectivityManager.stopAllServices();
    	super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags,  int startId)
    {
  		super.onStartCommand(intent, flags, startId);
  		
  		mBtAddress = intent.getStringExtra("btAddress");
  		mHttpPort = intent.getIntExtra("httpPort",8888);
  		mSocketPort = intent.getIntExtra("socketPort",7777);

  		if (null != mConnectivityManager)
  		{
  			mConnectivityManager.stopAllServices();
  		}
  		
  		mConnectivityManager = new ConnectivityManager(this);
  		mConnectivityManager.startAllServices(mBtAddress, mSocketPort, mHttpPort);
  		
  		return 0;
    }
    
    /**
     * Show a notification while this service is running.
     */
    private void showNotification()
    {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.connectivity_service_started);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.icon, text,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, activityMain.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.connectivity_service_started),
                       text, contentIntent);

        // Send the notification.
        mNM.notify(NOTIFICATION, notification);
    }
    
    private void showCustomNotification(String msg)
    {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = msg;

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.icon, text,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, activityMain.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.connectivity_service_started),
                       text, contentIntent);

        // Send the notification.
        mNM.notify(NOTIFICATION, notification);
    }
    
	@Override
	public IBinder onBind(Intent arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}
	
	public void sendBroadcast(String phoneIp, String bt, String socket, String http)
	{
		Intent lIntent = new Intent ("custom-event-name",null);
  		
		lIntent.putExtra("localIpAddress",phoneIp);
		lIntent.putExtra("statusBt",bt);
		lIntent.putExtra("statusSocket",socket);
		lIntent.putExtra("statusHttp",http);
		
        sendBroadcast(lIntent);
	}
}